//
//  BaseViewController.swift
//  weibo
//
//  Created by JonsonLiu on 2017/2/24.
//  Copyright © 2017年 JonsonLiu. All rights reserved.
//

import UIKit
import SnapKit

protocol BaseViewDelegate : NSObjectProtocol {
    func clickRegisterButton()
    func clickLoginButton()
}

class BaseView: UIView {

    
    weak var delegate : BaseViewDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func setupUI(){
        
        backgroundColor = UIColor.white
        addSubview(iconView)
        addSubview(createView)
        addSubview(messageLable)
        addSubview(leftButton)
        addSubview(rightButton)
        
        //房子约束
        iconView.translatesAutoresizingMaskIntoConstraints = false
//        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.centerX, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: NSLayoutAttribute.centerX, multiplier: 1, constant: 0))
//        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.centerY, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: NSLayoutAttribute.centerY, multiplier: 1, constant: 0))
        
        iconView.snp.makeConstraints { (make) in
            make.centerX.equalTo(self.snp.centerX)
            make.centerY.equalTo(self.snp.centerY)
        }
        //房子背景约束
        createView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: createView, attribute: .centerX, relatedBy: .equal, toItem: iconView, attribute: NSLayoutAttribute.centerX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: createView, attribute: .centerY, relatedBy: .equal, toItem: iconView, attribute: .centerY, multiplier: 1, constant: 0))
        //中间lable约束
        messageLable.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: messageLable, attribute: .top, relatedBy: .equal, toItem: createView, attribute: .bottom, multiplier: 1, constant: 5))
        addConstraint(NSLayoutConstraint(item: messageLable, attribute:.centerX, relatedBy: .equal, toItem: createView, attribute: .centerX, multiplier: 1, constant: 0))
        //注册按钮约束
        leftButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: leftButton, attribute: .left, relatedBy: .equal, toItem: messageLable, attribute: .left, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: leftButton, attribute: .top, relatedBy: .equal, toItem: messageLable, attribute: .bottom, multiplier: 1, constant: 10))
        addConstraint(NSLayoutConstraint(item: leftButton, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 100))
        
        //登陆按钮约束
        rightButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: rightButton, attribute: .right, relatedBy: .equal, toItem: messageLable, attribute: .right, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: rightButton, attribute: .top, relatedBy: .equal, toItem: messageLable, attribute: .bottom, multiplier: 1, constant: 10))
        addConstraint(NSLayoutConstraint(item: rightButton, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 100))
        
        
        
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.duration = 10
        anim.toValue = 2 * M_PI
        anim.isRemovedOnCompletion = false
        anim.repeatCount = MAXFLOAT
        createView.layer.add(anim, forKey: nil)
        
    }
    
    
    func registerClick(){
    
        delegate?.clickRegisterButton()
    }
    
    func loginClick(){
    
        delegate?.clickLoginButton()
    }
    
    
    private lazy var iconView : UIImageView = UIImageView(image:UIImage(named: "visitordiscover_feed_image_house"))
    
    private lazy var createView : UIImageView = UIImageView(image:UIImage(named:"visitordiscover_feed_image_smallicon"))
    private lazy var messageLable : UILabel = {
        
        let messageLable = UILabel()
        
        messageLable.text = "你说晴天和雨天的天气如何啊,其实我还是比较喜欢雨天"
        
        messageLable.font = UIFont.systemFont(ofSize: 18)
        
        messageLable.backgroundColor = UIColor.white
        
        messageLable.numberOfLines = 0
        
        messageLable.preferredMaxLayoutWidth = 240
        
        return messageLable
    }()
    
    private lazy var leftButton : UIButton = {
    
        let leftBUtton = UIButton(textColor: UIColor.black, fontSize: 20)
        leftBUtton.addTarget(self, action: #selector(BaseView.registerClick), for: .touchUpInside)
        leftBUtton.setTitle("注册", for: .normal)
    
    leftBUtton.setBackgroundImage(UIImage(named:"common_button_white"), for: .normal)
        
        return leftBUtton
    }()
    private lazy var rightButton : UIButton = {
        
        let rightBUtton = UIButton(textColor: UIColor.black, fontSize: 20)
        
        rightBUtton.addTarget(self, action: #selector(BaseView.loginClick), for: .touchUpInside)
        
        rightBUtton.setTitle("登陆", for: .normal)
        
        rightBUtton.setBackgroundImage(UIImage(named:"common_button_white"), for: .normal)
        
        return rightBUtton
    }()

    










}
